/** 
  *https://leetcode.cn/problems/koko-eating-bananas/submissions/565473429/
 * 875. 爱吃香蕉的珂珂
 * medium,  2024.9.14
 * 二分搜索
*/

class Solution {
public:
    bool check(vector<int>&piles,int H,int mid)
    {
        int sum=0;
        for(int i=0;i<piles.size();i++)
        {
            sum+=piles[i]%mid==0 ? piles[i]/mid : piles[i]/mid+1;
        }//关键核心思想
        return sum<=H;
    }
    
    int minEatingSpeed(vector<int>& piles, int H) {
        sort(piles.begin(),piles.end());
        int l=1;
        int r=piles[piles.size()-1];
        while(l<r)
        {
            int mid=l+(r-l)/2;
            if(check(piles,H,mid)){
                r=mid;
            }
            else{
                l=mid+1;
            }
        }
        return l;
    }
};